import os

from config.const import DataBaseConst

import os
from dotenv import find_dotenv, load_dotenv

# 加载.env文件到环境变量
load_dotenv(find_dotenv('.env'))


class BaseConfig:
    PROPAGATE_EXCEPTIONS = True
    DB_HOST = os.getenv("DB_HOST")
    DB_USERNAME = os.getenv("DB_USERNAME")
    DB_PASSWORD = os.getenv("DB_PASSWORD")

    SQLALCHEMY_ECHO = True  # 打印实际执行的sql语句：
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    FOR_OS_CONNECT = "mysql+pymysql://{user}:{password}@{address}:3306/{db}?charset=utf8mb4".format(
        user=DB_USERNAME, password=DB_PASSWORD, address=DB_HOST, db=DataBaseConst.FOR_OS
    )

    SQLALCHEMY_DATABASE_URI = "mysql+pymysql://{user}:{password}@{address}:3306/{db}?charset=utf8mb4".format(
        user=DB_USERNAME,
        password=DB_PASSWORD,
        address=DB_HOST,
        db=DataBaseConst.SILO,
    )

    SQLALCHEMY_BINDS = {"for_os": FOR_OS_CONNECT}


class TestConfig(BaseConfig):
    TESTING = True

    DB_HOST = os.getenv("PYTEST_DB_HOST")
    DB_USERNAME = os.getenv("PYTEST_DB_USERNAME")
    DB_PASSWORD = os.getenv("PYTEST_DB_PASSWORD")
    DB_NAME = os.getenv("PYTEST_DB_DATABASE_SOME", "database_name")

    SQLALCHEMY_TRACK_MODIFICATIONS = False

    # for_os
    FOR_OS_CONNECT = "mysql+pymysql://{user}:{password}@{address}:3306/{db}?charset=utf8mb4".format(
        user=DB_USERNAME, password=DB_PASSWORD, address=DB_HOST, db=DataBaseConst.FOR_OS
    )

    SQLALCHEMY_DATABASE_URI = "mysql+pymysql://{user}:{password}@{address}:3306/{db}?charset=utf8mb4".format(
        user=DB_USERNAME,
        password=DB_PASSWORD,
        address=DB_HOST,
        db=DataBaseConst.NINTH_STUDIO,
    )

    SQLALCHEMY_BINDS = {"for_os": FOR_OS_CONNECT}


class DevConfig(BaseConfig):
    DEBUG = True

    DB_HOST = os.getenv("DB_HOST")
    DB_USERNAME = os.getenv("DB_USERNAME")
    DB_PASSWORD = os.getenv("DB_PASSWORD")
    DB_NAME = os.getenv("DB_NAME")

    SQLALCHEMY_TRACK_MODIFICATIONS = False

    # for_os
    FOR_OS_CONNECT = "mysql+pymysql://{user}:{password}@{address}:3306/{db}?charset=utf8mb4".format(
        user=DB_USERNAME, password=DB_PASSWORD, address=DB_HOST, db=DB_NAME
    )

    SQLALCHEMY_DATABASE_URI = "mysql+pymysql://{user}:{password}@{address}:3306/{db}?charset=utf8mb4".format(
        user=DB_USERNAME,
        password=DB_PASSWORD,
        address=DB_HOST,
        db=DataBaseConst.SILO,
    )

    SQLALCHEMY_BINDS = {"for_os": FOR_OS_CONNECT}


config = {
    "local": BaseConfig,
    "debug": BaseConfig,
    "dev": BaseConfig,
    "test": BaseConfig,
    "stage": BaseConfig,
    "prod": BaseConfig,
    "localprod": BaseConfig,
    "pytest": TestConfig,
    None: BaseConfig,
}
